//@charset "utf-8";
//问句   超过300条
define(function (require, exports, module) {
	var Backbone = require('backbone');

	var secondSearchModel = require('../models/secondsearch');

	var SecondSearchView = Backbone.View.extend({
		el : '#view_result_condition_second',
		query : '',
		initialize : function() {
			//
		},
		events : {
			'click .view_second_option' : 'changeSecondOption'
		},
		querySearchView : null,
		request : function (qid, token, query) {
			this.query = query;
			var self  = this;
			secondSearchModel.getConditionList(qid, token, query, function () {
				self.render();
			});
		},
		render : function () {
			var errorCode = secondSearchModel.get('errorcode');
			
			if (errorCode === 0) {
				var result = secondSearchModel.get('result');
				var html = ['<div class="condition-arrow"></div>'];
				var index = 1;
				for (var i in result) {
					var name = i;
					var data = [];
					for (var j in result[i]) {
						data.push(result[i][j][0]);
					}

					var row = [
						'<div class="condition">'+
						'<span class="condition-title">'+name+'：</span>'
					];
					var inIndex = 1;
					
					for (var h in data) {
						row.push('<span class="keyword view_second_option" title="'+data[h]+'">'+data[h]+'</span>');
						if (inIndex++ >= 3) {
							break;
						}
					}
					row.push('</div>');
					html.push(row.join(''));
					if (index++ >= 3) {
						break;
					}
				}
				this.$el.html(html.join('')).show();
			}
		},
		changeSecondOption : function (e) {
			TA.log({id : '57909949_314'});
			e = e || window.event;
			var $currDom = $(e.currentTarget);
			if ($currDom.hasClass('selected')) {
				$currDom.removeClass('selected');
				this.querySearchView.request(this.query);
				return ;
			}

			// $currDom.parent().find('.selected').removeClass('selected');
			$currDom.addClass('selected');
			
			this.querySearchView.request(this.query);
		},
		getSelectedOptions : function (query) {
			var secondList = [];
			secondList.push(query);
			$('.view_second_option.selected').each(function () {
				secondList.push($(this).html());
			});
			return secondList.join('，');
		}
	});

	module.exports = new SecondSearchView;
});